package com.example.java_chatroom.api;

import com.example.java_chatroom.model.Friend;
import com.example.java_chatroom.model.FriendMapper;
import com.example.java_chatroom.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;


@RestController
public class FriendAPI {
    @Autowired
    FriendMapper friendMapper;

    @GetMapping("/friendList")
    public Object getFriendList(HttpServletRequest req, @SessionAttribute("user") User user) {
//        // 1. 先从会话中获取到 userId
//        HttpSession session = req.getSession(false);
//        if (session == null) {
//            // 当前用户未登录 -> 会话不存在
//            System.out.println("[getFriendList] session 不存在");
//            return new ArrayList<Friend>();
//        }
//
//        User user = (User) session.getAttribute("user");
//        if (user == null) {
//            // 当前用户对象在会话中不存在
//            System.out.println("[getFriendList] user 不存在");
//            return new ArrayList<Friend>();
//        }

        // 2. 根据 userId 从数据库中查询数据即可
        return friendMapper.selectFriendList(user.getUserId());
    }
}
